package main

import (
	"net/http"
	"net/url"
	"path"
	"strings"
)

func getFileNameFromUrl(url *url.URL) string {
	base := path.Base(url.Path)
	if base == "." || base == "/" {
		return "index.html" // 默认文件名
	}
	return base
}

func getFileNameFromHeader(h http.Header) string {
	contentDisposition := h.Get("Content-Disposition")

	if contentDisposition == "" {
		return ""
	}

	// 解析 Content-Disposition 头部
	parts := strings.Split(contentDisposition, ";")
	for _, part := range parts {
		part = strings.TrimSpace(part)
		if strings.HasPrefix(part, "filename=") {
			filename := strings.TrimPrefix(part, "filename=")
			filename = strings.Trim(filename, `"`) // 去掉双引号
			return filename
		}
	}
	return ""
}
